#define _SVOD_SOURCE
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>

#include <string.h>
#include <stdarg.h>
#include <errno.h>
#include <assert.h>
#include <ctype.h>
#include <time.h>
#include <math.h>
#include "common.h"
#include "strutils.h"
#include "strlist.h"
#include "utils2.h"

#include "base64.h"
#include "crc16.h"
#include "protocmd.h"
#include "dataapi.h"


//以下函数声明未出现在其他头文件中，防止警告，须在此声明
int GetBit(BYTE *Buffer, int Index);
int cbFillin(void *data, int idx, void *param);
int cbReplace(void *data, int idx, void *param);
int AppendRec(void *data, int size, int fd);

int getRTLog(TBuffer *buffer);




//--------------------------- http ---------------------------------------//

int lowercase(const char *s)
{
        return (tolower(* (unsigned char *) s));
}

int mg_strncasecmp(const char *s1, const char *s2, size_t len)
{
        int diff = 0;

        if (len > 0)
                do
                {
                        diff = lowercase(s1++) - lowercase(s2++);
                }while (diff == 0 && s1[-1] != '\0' && --len > 0);

        return diff;
}

int mg_strcasecmp(const char *s1, const char *s2)
{
        int diff;

        do
        {
                diff = lowercase(s1++) - lowercase(s2++);
        }while (diff == 0 && s1[-1] != '\0');

        return diff;
}

/*
 * URL-decode input buffer into destination buffer.
 * 0-terminate the destination buffer. Return the length of decoded data.
 * form-url-encoded data differs from URI encoding in a way that it
 * uses '+' as character for space, see RFC 1866 section 8.2.1
 * http://ftp.ics.uci.edu/pub/ietf/html/rfc1866.txt
 */
size_t url_decode(const char *src, size_t src_len, char *dst, size_t dst_len,UDK_BOOL is_form_url_encoded)
{
        size_t i, j;
        int a, b;
#define HEXTOI(x)  (isdigit(x) ? x - '0' : x - 'W')

        for (i = j = 0; i < src_len && j < dst_len - 1; i++, j++)
        {
                if (src[i] == '%' && isxdigit(* (unsigned char *) (src + i + 1)) && isxdigit(* (unsigned char *) (src + i + 2)))
                {
                        a = tolower(* (unsigned char *) (src + i + 1));
                        b = tolower(* (unsigned char *) (src + i + 2));
                        dst[j] = ((HEXTOI(a) << 4) | HEXTOI(b)) & 0xff;
                        i += 2;
                }
                else
                if (is_form_url_encoded && src[i] == '+')
                {
                        dst[j] = ' ';
                }
                else
                {
                        dst[j] = src[i];
                }
        }

        dst[j] = '\0';
        /* Null-terminate the destination */

        return j;
}


/*
 * Search for a form variable in a given buffer.
 * Semantic is the same as for mg_get_var().
 */
char *get_var(const char *name, const char *buf, size_t buf_len)
{
        const char *p, *e, *s;
        char *val;
        size_t var_len, len;

        var_len = strlen(name);
        e = buf + buf_len;
        val = NULL;

        /* buf is "var1=val1&var2=val2...". Find variable first */
        for (p = buf; p + var_len < e; p++)
                if ((p == buf || p[-1] == '&') && p[var_len] == '=' && !mg_strncasecmp(name, p, var_len))
                {
                        /* Point p to variable value */
                        p += var_len + 1;

                        /* Point s to the end of the value */
                        s = (const char *) memchr(p, '&', e - p);
                        if (s == NULL)
                                s = e;

                        /* Try to allocate the buffer */
                        len = s - p;
                        if ((val = (char *) malloc(len + 1)) != NULL)
                                url_decode(p, len, val, len + 1, true);
                        break;
                }

        return val;
}

//--------------------------- http end ---------------------------------------//



int dataURI(const char *query, TBuffer *buffer)
{
	int ret, qlen=query?strlen(query):0;
	char *action, *table, *fields, *filter, *options;
	if(qlen<=0) return ERROR_QUERY;
	table[0]=0; filter[0]=0; fields[0]=0; options[0]=0;

//	printf("URL: %s  %s\n", query, data);
	//提取各种类型数据
	if(!(action=get_var("Action", query, qlen)))
		return ERROR_PARAM;

	table = get_var("Table", query, qlen);
	fields = get_var("Fields", query, qlen);
	filter = get_var("Filter", query, qlen);
	options = get_var("Options", query, qlen);

	if(buffer)
	{
		ret=sprintf((char *)buffer->bufPtr,
			"Action=%s\nTable=%s\nFields=%s\nFilter=%s\nOptions=%s\n",
			action, table, fields, filter, options
			);
		buffer->bufPtr+=ret;
	}
	return ERROR_PARAM;
}

static MAP_BIT_OP mapBitOP[] = {
	{BIT_OP_EQU,			STR_OP_EQU				},
	{BIT_OP_IN,				STR_OP_IN				},
	{BIT_OP_BETWEN,			STR_OP_BETWEN			},
	{BIT_OP_LESS_THAN,		STR_OP_LESS_THAN		},
	{BIT_OP_MORE_THAN,		STR_OP_MORE_THAN		},
	{BIT_OP_LESS_THAN_EQU,	STR_OP_LESS_THAN_EQU	},
	{BIT_OP_MORE_THAN_EQU,	STR_OP_MORE_THAN_EQU	},
	{BIT_OP_LIKE,			STR_OP_LIKE				},
	{BIT_OP_HAS,			STR_OP_HAS				}
};

char *StrRev(char *s)
{
	int j,k,c;
	for(k=0;s[k] != 0;k++);
	for(j=0,k--;j<k;j++,k--) {
		c = s[j];
		s[j] = s[k];
		s[k] = c;
	}
	return s;
}

//删除一个字符串的前面一组空格
void SkipSpace(char **pptr)
{
	while ( isspace((int)(unsigned char)*(*pptr))) 
		++(*pptr);
}
//删除一个字符串的最后一组空格
void DelLastSpace(char *pptr)
{
	int n= strlen(pptr);
	char *p = (pptr) + n -1;

	while (isspace(*p)) p--;
	*(p+1) = '\0';
}

char *trimSpace(char **pptr)
{
	DelLastSpace(*pptr);
	SkipSpace(pptr);
	return *pptr;
}

//删除一个字符串的一类字符
void SkipChar(char **pptr, char ch)
{
	while (*(*pptr) == ch) 
		++(*pptr);
}
//删除一个字符串的最后一类字符
void DelLastChar(char *pptr, char ch)
{
	int n= strlen(pptr);
	char *p = (pptr) + n -1;

	while ((*p) == ch) 
		p--;
	*(p+1) = '\0';
}

char *trimChar(char **pptr, char ch)
{
	DelLastChar(*pptr, ch);
	SkipChar(pptr, ch);
	return *pptr;
}

bool IsWordTrail(char ch)
{
	if(ch == ' ' || ch == '=' || ch == '>' || ch == '<' || ch == '(') 
		return true;


	return false;

}


bool IsOpTrail(char ch)
{
	if(ch == '=') return false;
	return true;
}



 

